<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://code.google.com/p/feather-cm
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

define('PUBLISH_DRAFT',  -1);
define('PUBLISH_PRIVATE', 0);
define('PUBLISH_ONLINE',  1);

class PublishComponent
{
	public $status;
	
	function startup(Controller $controller)
	{
		$this->status = array(
#			PUBLISH_DRAFT   => _('Draft'),
#			PUBLISH_ONLINE  => _('Published'),
			PUBLISH_ONLINE  => '',
			PUBLISH_PRIVATE => _('Private'),
		);
	}
	
	function & getList()
	{
		$status = array(
			PUBLISH_ONLINE  => _('Publicly available'),
			PUBLISH_PRIVATE => _('Keep private')
		);
		return $status;
	}
	
	function & getFullList()
	{
		$status = array(
#			PUBLISH_DRAFT   => _('Draft'),
			PUBLISH_PRIVATE => _('Private'),
			PUBLISH_ONLINE  => _('Published')
		);
		return $status;
	}
	
	function getStatus($status, $created=null)
	{
		if ($created < Time::SQL())
		{
			switch ($status)
			{
				case PUBLISH_ONLINE:  return;
				case PUBLISH_PRIVATE: $class = 'private';
				case PUBLISH_DRAFT:   $class = 'draft';   break;
			}
			$text  = $this->status[$status];
			$title = '';
		}
		else
		{
			$text  = _('Pre-Published');
			$class = 'future';
			$title = 'title="'.Time::niceShort($created).'"';
		}
		return "<span class=\"$class\" $title><span>[$text]</span></span>";
	}
	
	function getName($status, $created=null)
	{
		return ($created < Time::SQL()) ? $this->status[$status] : _('Pre-Published');
	}
	
	function getClass($status, $created=null, $full=false)
	{
		if ($created >= Time::SQL())
		{
			$class = 'future';
			$title = 'title="'.ucfirst(Time::niceShort($created)).'"';
		}
		else
		{
			switch ($status)
			{
				case PUBLISH_ONLINE:  return;
				case PUBLISH_DRAFT:   $class = 'draft';   break;
				case PUBLISH_PRIVATE: $class = 'private';
			}
			$title = '';
		}
		return $full ? "class=\"$class\" $title" : $class;
	}
	
	function displaySpans($status, $created, $url)
	{
		$str = ($status == PUBLISH_PRIVATE) ?
			'<a class="private" href="'.$url.'/published/1" title="'._('Make it public').'"><span>'._('Private').'</span></a>':
			'<a class="public"  href="'.$url.'/published/0" title="'._('Make it private').'"><span>'._('Public').'</span></a>';
		if ($created >= Time::SQL())
		{
			$str .= '<span class="future '.$created.'" title="'.
				sprintf(_('This item will be published on %s'), Time::niceShort($created)).
				'"><span>'._('Prepublished').'</span></span>';
		}
		return $str;
	}
}
?>